package com.itttq.gshop.admin.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.connector.ISysUserConnector;
import com.itttq.gshop.generic.util.Constants;

@RestController
@RequestMapping("sys/user")
public class SysUserController extends GenericController {
	private static final long serialVersionUID = -18560338577618623L;
	@Autowired
	private ISysUserConnector sysUserConnector;
	
	/**
	 * 用户登录
	 * 
	 * @param username
	 * @param password
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public ModelAndView login(String username, String password, HttpServletRequest request) {
		ModelAndView mv= new ModelAndView("login");
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			mv.addObject("msg", "用户名或密码不能为空！");
			return mv;
		}
		SysUserPo user = new SysUserPo();
		user.setUsername(username);
		ResultPageTo<SysUserPo> rpt = sysUserConnector.getSysUsers(user);
		// 放入session
		if(null == rpt || rpt.getCode() != ResultTo.SUCCESS) {
			mv.addObject("msg", "用户名或密码错误！");
		} else {
			this.setSession(request, Constants.SESSION_USER, user);
			mv.setViewName("redirect:/main.html");
		}
		return mv;
	}

	/**
	 * 根据条件查询对象，并分页
	 * 
	 * @param user
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("users")
	public ModelAndView getSysUsers(SysUserPo user, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/sys/users");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		user.setStart(ppt.getStart());
		user.setSize(ppt.getSize());
		ResultPageTo<SysUserPo> rpt = sysUserConnector.getSysUsers(user);
		this.getPageMv("users", mv, rpt);
		mv.addObject("user", user);
		return mv;
	}

	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add")
	public ModelAndView addSysUser() {
		ModelAndView mv = new ModelAndView("/sys/userAdd");
		mv.addObject("op", "add");
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}")
	public ModelAndView viewSysUser(@PathVariable("id") Integer id) {
		ModelAndView mv = addSysUser();
		if(id != null) {
			mv.addObject("user", sysUserConnector.getSysUser(id));
		}
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}")
	public ModelAndView editSysUser(@PathVariable("id") Integer id) {
		ModelAndView mv = viewSysUser(id);
		mv.addObject("op", "edit");
		return mv;
	}
	
	/**
	 * 保存对象，无ID则新增，有ID则更新
	 * 
	 * @param user
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save")
	public ModelAndView saveSysUser(SysUserPo user) {
		sysUserConnector.saveSysUser(user);
		return getSysUsers(new SysUserPo(), null);
	}

	/**
	 * 根据ID删除对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}")
	@ResponseBody
	public ModelAndView delSysUser(@PathVariable("id") Integer id) {
		if(id != null) {
			sysUserConnector.delSysUser(id);
		}
		return getSysUsers(new SysUserPo(), null);
	}
}
